package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.oscar.ehome.MainActivity;
import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.DuDaoBean;
import com.oscar.ehome.bean.pagebean.ZaoHuiList;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.utils.dateview.DateDialog;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;
import com.oscar.ehome.utils.xrefresh.XRefreshView;
import com.oscar.ehome.utils.xrefresh.XScrollView;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.adapter.DuDaoAdapter;
import com.oscar.ehome.view.pageview.view.DuDaoView;
import com.oscar.ehome.view.setview.view.CustomCalendar;
import com.oscar.ehome.view.setview.view.DayFinish;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/12/21
 */

public class DuDaoAtivity extends BasesActivity<DuDaoView, PagePresenter> implements DuDaoView, XRefreshView.XRefreshViewListener,
        AdapterView.OnItemClickListener, ProgressFrameLayout.AgainRequestListener {


    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    @BindView(R.id.title_tv_menu)
    TextView titleTvMenu;
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    @BindView(R.id.img_spinner)
    ImageView imgSpinner;
    @BindView(R.id.liear_title)
    LinearLayout liearTitle;
    @BindView(R.id.title_tv_right)
    TextView titleTvRight;
    @BindView(R.id.title_img_right)
    ImageView titleImgRight;
    @BindView(R.id.title_img_right_new)
    ImageView titleImgRightNew;

    @BindView(R.id.home_tv_num)
    TextView homeTvNum;
    @BindView(R.id.title_right_layput)
    RelativeLayout titleRightLayput;
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    @BindView(R.id.sgin_date)
    TextView sginDate;
    @BindView(R.id.spiner_sgin_img)
    ImageView spinerSginImg;
    @BindView(R.id.spiner_sginlist)
    RelativeLayout spinerSginlist;
    @BindView(R.id.cal)
    CustomCalendar cal;
    @BindView(R.id.yaoyueqingkuang)
    TextView yaoyueqingkuang;
    @BindView(R.id.daohuilv)
    TextView daohuilv;
    @BindView(R.id.daohuilvbaifenbi)
    TextView daohuilvbaifenbi;
    @BindView(R.id.goupiaozhutiedit)
    EditText goupiaozhutiedit;
    @BindView(R.id.textView2)
    TextView textView2;
    @BindView(R.id.daohuizhuti)
    EditText daohuizhuti;
    @BindView(R.id.daocahngkehu)
    EditText daocahngkehu;
    @BindView(R.id.chengjiaozhuti)
    EditText chengjiaozhuti;
    @BindView(R.id.huomingdanzhuti)
    EditText huomingdanzhuti;
    @BindView(R.id.shuangshouzhuti)
    EditText shuangshouzhuti;
    @BindView(R.id.kehuchengjiaomingxi)
    TextView kehuchengjiaomingxi;
    @BindView(R.id.kehuchengjiaomingxibaifenbi)
    TextView kehuchengjiaomingxibaifenbi;
    @BindView(R.id.chengjiaorenshu)
    EditText chengjiaorenshu;
    @BindView(R.id.danshu)
    EditText danshu;
    @BindView(R.id.baofei)
    EditText baofei;
    @BindView(R.id.textView3)
    TextView textView3;
    @BindView(R.id.zhuanjieshaobaifenbi)
    TextView zhuanjieshaobaifenbi;
    @BindView(R.id.lingjuanrenshu)
    EditText lingjuanrenshu;
    @BindView(R.id.zhuanjieshaorenshu)
    EditText zhuanjieshaorenshu;
    @BindView(R.id.mingdanshu)
    EditText mingdanshu;
    @BindView(R.id.chengjiaozengyuanshu)
    EditText chengjiaozengyuanshu;
    @BindView(R.id.zaoganhuiedit)
    EditText zaoganhuiedit;
    @BindView(R.id.dazaohuiedit)
    EditText dazaohuiedit;
    @BindView(R.id.shidongrenshuedit)
    EditText shidongrenshuedit;
    @BindView(R.id.shujuzhanshi)
    LinearLayout shujuzhanshi;
    @BindView(R.id.chuangkeshuju)
    LinearLayout chuangkeshuju;
    @BindView(R.id.zaohuishangbao)
    LinearLayout zaohuishangbao;
    @BindView(R.id.scollview_dudao)
    XScrollView scollviewDudao;
    @BindView(R.id.xuanfuanniu)
    ImageButton xuanfuanniu;
    @BindView(R.id.insura_fragment)
    ProgressFrameLayout insuraFragment;
    private List<DuDaoBean.DuDaoxinxi> list = new ArrayList<>();
    private List<ZaoHuiList.riqi> zaohuilist = new ArrayList<>();

    PagePresenter presenter;
    String token = "";
    String dates = "";
    String year = "";
    String mouth = "";
    int mYear;
    int mMonth;
    int date;
    int bianliang;
    final List<DayFinish> lists = new ArrayList<>();
    String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dudaolist);
        ButterKnife.bind(this);
        cal.setFocusable(true);
        shujuzhanshi.setVisibility(View.GONE);
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(DuDaoAtivity.this).getSystemConfig().getColor()));
        initView();
        onClick();
    }

    /**
     * 初始化
     */
    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText("数据上报");
        Intent in = getIntent();
        type = in.getStringExtra("type");
        token = SystemConfigFactory.getInstance(DuDaoAtivity.this).getSystemConfig().getToken();
        presenter = new PagePresenter(this);
        Calendar c = Calendar.getInstance();//
        mYear = c.get(Calendar.YEAR); // 获取当前年份
        mMonth = c.get(Calendar.MONTH) + 1;// 获取当前月份
        date = c.get(Calendar.DATE);
        year = mYear + "";
        mouth = mMonth + "";
        dates = mYear + "-" + mMonth;
        sginDate.setText(dates);

        goupiaozhutiedit.setFocusable(false);
        daohuizhuti.setFocusable(false);
        daocahngkehu.setFocusable(false);
        chengjiaozhuti.setFocusable(false);
        chengjiaorenshu.setFocusable(false);
        danshu.setFocusable(false);
        baofei.setFocusable(false);
        lingjuanrenshu.setFocusable(false);
        zhuanjieshaorenshu.setFocusable(false);
        mingdanshu.setFocusable(false);
        chengjiaozengyuanshu.setFocusable(false);
        shuangshouzhuti.setFocusable(false);
        huomingdanzhuti.setFocusable(false);
        zaoganhuiedit.setFocusable(false);
        dazaohuiedit.setFocusable(false);
        shidongrenshuedit.setFocusable(false);

    }


    /**
     * 加载数据
     */
    public void initData() {
        if (isNetWork()) {
            insuraFragment.stopRefresh();
            if (type.equals("2")) {
                presenter.getdudaolist(token, dates);
            } else {
                //zaohui
                presenter.getzaohuilist(token, dates);
            }

            showLoading();
            cal.setOnClickListener(new CustomCalendar.onClickListener() {
                @Override
                public void onDayClick(int day) {
                    if (type.equals("1")) {
                        if (zaohuilist.get(day - 1).getPunchcardReport() != null && !zaohuilist.get(day - 1).getPunchcardReport().equals("")) {
                            shujuzhanshi.setVisibility(View.VISIBLE);
                            zaohuishangbao.setVisibility(View.VISIBLE);
                            fuzhifun(day - 1);

                        } else {
                            zaohuishangbao.setVisibility(View.GONE);
                            chuangkeshuju.setVisibility(View.GONE);
                            shujuzhanshi.setVisibility(View.GONE);
                        }
                    } else {
                        if (list.get(day - 1).getReportedRecord() != null && !list.get(day - 1).getReportedRecord().equals("")) {
                            shujuzhanshi.setVisibility(View.VISIBLE);


                            chuangkeshuju.setVisibility(View.VISIBLE);

                            fuzhifun(day - 1);

                        } else {
                            zaohuishangbao.setVisibility(View.GONE);
                            chuangkeshuju.setVisibility(View.GONE);
                            shujuzhanshi.setVisibility(View.GONE);
                        }
                    }

                }
            });
            showLoading();
        } else {
            insuraFragment.setVisibility(View.VISIBLE);
            insuraFragment.setLoadFail();

        }

    }

    public void fuzhifun(int i) {
        bianliang = i;
        if (type.equals("1")) {
            //zaohui
            if (zaohuilist.get(bianliang).getPunchcardReport().getNumber1() != 0.0) {
                zaoganhuiedit.setText((int) zaohuilist.get(bianliang).getPunchcardReport().getNumber1() + "");
            } else {
                zaoganhuiedit.setText("0");
            }
            if (zaohuilist.get(bianliang).getPunchcardReport().getNumber2() != 0.0) {
                dazaohuiedit.setText((int) zaohuilist.get(bianliang).getPunchcardReport().getNumber2() + "");
            } else {
                dazaohuiedit.setText("0");
            }
            if (zaohuilist.get(bianliang).getPunchcardReport().getNumber3() != 0.0) {
                shidongrenshuedit.setText((int) zaohuilist.get(bianliang).getPunchcardReport().getNumber3() + "");
            } else {
                shidongrenshuedit.setText("0");
            }
        } else {
            if (list.get(bianliang).getReportedRecord().getTicketMain() != 0.0) {
                goupiaozhutiedit.setText((int) list.get(bianliang).getReportedRecord().getTicketMain() + "");
            } else {
                daohuizhuti.setText("0");
            }
            if (list.get(bianliang).getReportedRecord().getBodyMain() != 0.0) {
                daohuizhuti.setText((int) list.get(bianliang).getReportedRecord().getBodyMain() + "");
            } else {
                daohuizhuti.setText("0");
            }
            if (list.get(bianliang).getReportedRecord().getGuestsPresent() != 0.0) {
                daocahngkehu.setText((int) list.get(bianliang).getReportedRecord().getGuestsPresent() + "");
            } else {
                daocahngkehu.setText("0");
            }
            if (list.get(bianliang).getReportedRecord().getTransactionMain() != 0.0) {
                chengjiaozhuti.setText((int) list.get(bianliang).getReportedRecord().getTransactionMain() + "");
            } else {
                chengjiaozhuti.setText("0");
            }
            if (list.get(bianliang).getReportedRecord().getMainList() != 0.0) {
                huomingdanzhuti.setText((int) list.get(bianliang).getReportedRecord().getMainList() + "");
            } else {
                huomingdanzhuti.setText("0");
            }
            if (list.get(bianliang).getReportedRecord().getTransactionNumber() != 0.0) {
                chengjiaorenshu.setText((int) list.get(bianliang).getReportedRecord().getTransactionNumber() + "");
            } else {
                chengjiaorenshu.setText("0");
            }
            if (list.get(bianliang).getReportedRecord().getOrderNumber() != 0.0) {
                danshu.setText((int) list.get(bianliang).getReportedRecord().getOrderNumber() + "");
            } else {
                danshu.setText("0");
            }
            if (list.get(bianliang).getReportedRecord().getDoubleIncomeMainBody() != 0.0) {
                shuangshouzhuti.setText((int) list.get(bianliang).getReportedRecord().getDoubleIncomeMainBody() + "");
            } else {
                shuangshouzhuti.setText("0");
            }
            if (list.get(bianliang).getReportedRecord().getPreminu() != null && list.get(bianliang).getReportedRecord().getPreminu() != 0.0) {
                baofei.setText(list.get(bianliang).getReportedRecord().getPreminu() + "");
            } else {
                baofei.setText("0");
            }
            if (list.get(bianliang).getReportedRecord().getVoucherNumber() != 0.0) {
                lingjuanrenshu.setText((int) list.get(bianliang).getReportedRecord().getVoucherNumber() + "");
            } else {
                lingjuanrenshu.setText("0");
            }
            if (list.get(bianliang).getReportedRecord().getReferralNumber() != 0.0) {
                zhuanjieshaorenshu.setText((int) list.get(bianliang).getReportedRecord().getReferralNumber() + "");
            } else {
                zhuanjieshaorenshu.setText("0");
            }
            if (list.get(bianliang).getReportedRecord().getListNumber() != 0.0) {
                mingdanshu.setText((int) list.get(bianliang).getReportedRecord().getListNumber() + "");
            } else {
                mingdanshu.setText("0");
            }
            if (list.get(bianliang).getReportedRecord().getIncreaseNumber() != 0.0) {
                chengjiaozengyuanshu.setText((int) list.get(bianliang).getReportedRecord().getIncreaseNumber() + "");
            } else {
                chengjiaozengyuanshu.setText("0");
            }
            if (list.get(bianliang).getReportedRecord().getToRate() != null && !list.get(bianliang).getReportedRecord().getToRate().equals("")) {
                daohuilv.setVisibility(View.VISIBLE);
                daohuilvbaifenbi.setVisibility(View.VISIBLE);
                daohuilvbaifenbi.setText(list.get(bianliang).getReportedRecord().getToRate());
            } else {
                daohuilv.setVisibility(View.VISIBLE);
                daohuilvbaifenbi.setVisibility(View.VISIBLE);
                daohuilvbaifenbi.setText("0.0%");
            }
            if (list.get(bianliang).getReportedRecord().getTurnoverRate() != null && !list.get(bianliang).getReportedRecord().getTurnoverRate().equals("")) {
                kehuchengjiaomingxibaifenbi.setVisibility(View.VISIBLE);
                kehuchengjiaomingxi.setVisibility(View.VISIBLE);
                kehuchengjiaomingxibaifenbi.setText(list.get(bianliang).getReportedRecord().getTurnoverRate());
            } else {
                kehuchengjiaomingxibaifenbi.setVisibility(View.VISIBLE);
                kehuchengjiaomingxi.setVisibility(View.VISIBLE);
                kehuchengjiaomingxibaifenbi.setText("0.0%");
            }
            if (list.get(bianliang).getReportedRecord().getReferralRate() != null && !list.get(bianliang).getReportedRecord().getReferralRate().equals("")) {
                zhuanjieshaobaifenbi.setVisibility(View.VISIBLE);
                textView3.setVisibility(View.VISIBLE);
                zhuanjieshaobaifenbi.setText(list.get(bianliang).getReportedRecord().getReferralRate());
            } else {
                zhuanjieshaobaifenbi.setVisibility(View.VISIBLE);
                textView3.setVisibility(View.VISIBLE);
                zhuanjieshaobaifenbi.setText("0.0%");
            }
        }

    }

    public void onClick() {


        insuraFragment.setAgainRequestListener(this);
    }

    @OnClick({R.id.title_layout_left, R.id.spiner_sginlist, R.id.xuanfuanniu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_layout_left://返回
                finish();
                break;
            case R.id.spiner_sginlist:
                getDate();
                break;
            case R.id.xuanfuanniu:
                scollviewDudao.smoothScrollTo(0, 0);
                break;
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void getDuDaoList(DuDaoBean result) {
        dismissLoading();
        //   数据进行加载

        if (result != null) {
            lists.clear();
            list = result.getList();
            if (dates.equals(mYear + "-" + mMonth)) {  //当前月份  默认加载当日数据
                if (list.get(date - 1).getReportedRecord() != null && !list.get(date - 1).getReportedRecord().equals("")) {
                    shujuzhanshi.setVisibility(View.VISIBLE);
                    chuangkeshuju.setVisibility(View.VISIBLE);
                    zaohuishangbao.setVisibility(View.GONE);
                    fuzhifun(date - 1);
                } else {
                    chuangkeshuju.setVisibility(View.GONE);
                    zaohuishangbao.setVisibility(View.GONE);
                    shujuzhanshi.setVisibility(View.GONE);
                }
            } else {//  不是当前月份 默认加载一号数据
                if (list.get(0).getReportedRecord() != null && !list.get(0).getReportedRecord().equals("")) {
                    shujuzhanshi.setVisibility(View.VISIBLE);
                    chuangkeshuju.setVisibility(View.VISIBLE);
                    zaohuishangbao.setVisibility(View.GONE);
                    fuzhifun(0);

                } else {

                    chuangkeshuju.setVisibility(View.GONE);
                    zaohuishangbao.setVisibility(View.GONE);
                    shujuzhanshi.setVisibility(View.GONE);

                }
            }

            //x循环加载当月数据情况   1  有数据  2   无数据
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getReportedRecord() != null && !list.get(i).getReportedRecord().equals("")) {
                    lists.add(new DayFinish(1, 2, 2));
                } else {
                    lists.add(new DayFinish(0, 2, 2));
                }
            }
            cal.setRenwu(year + "年" + mouth + "月", lists);
            Log.e("gaodu", "getDuDaoList: " + (cal.getHeight() + spinerSginlist.getHeight()));
            scollviewDudao.smoothScrollTo(0, 20);
        } else {
        }

    }

    @Override
    public void getzaohuilist(ZaoHuiList result) {
        dismissLoading();
        xuanfuanniu.setVisibility(View.GONE);
        //   数据进行加载
        if (result != null) {
            zaohuilist.clear();
            zaohuilist = result.getList();
            if (dates.equals(mYear + "-" + mMonth)) {  //当前月份  默认加载当日数据
                if (zaohuilist.get(date - 1).getPunchcardReport() != null && !zaohuilist.get(date - 1).getPunchcardReport().equals("")) {
                    shujuzhanshi.setVisibility(View.VISIBLE);
                    chuangkeshuju.setVisibility(View.GONE);
                    zaohuishangbao.setVisibility(View.VISIBLE);
                    fuzhifun(date - 1);
                } else {
                    chuangkeshuju.setVisibility(View.GONE);
                    zaohuishangbao.setVisibility(View.GONE);
                    shujuzhanshi.setVisibility(View.GONE);
                }
            } else {//  不是当前月份 默认加载一号数据
                if (zaohuilist.get(0).getPunchcardReport() != null && !zaohuilist.get(0).getPunchcardReport().equals("")) {
                    shujuzhanshi.setVisibility(View.VISIBLE);
                    chuangkeshuju.setVisibility(View.GONE);
                    zaohuishangbao.setVisibility(View.VISIBLE);
                    fuzhifun(0);

                } else {
                    chuangkeshuju.setVisibility(View.GONE);
                    zaohuishangbao.setVisibility(View.GONE);
                    shujuzhanshi.setVisibility(View.GONE);

                }
            }

            //x循环加载当月数据情况   1  有数据  2   无数据
            for (int i = 0; i < zaohuilist.size(); i++) {
                if (zaohuilist.get(i).getPunchcardReport() != null && !zaohuilist.get(i).getPunchcardReport().equals("")) {
                    lists.add(new DayFinish(1, 2, 2));
                } else {
                    lists.add(new DayFinish(0, 2, 2));
                }
            }
            cal.setRenwu(year + "年" + mouth + "月", lists);
            Log.e("gaodu", "getDuDaoList: " + (cal.getHeight() + spinerSginlist.getHeight()));
            scollviewDudao.smoothScrollTo(0, 20);
        } else {
        }
    }

    @Override
    public void erroy() {
        dismissLoading();

        ToastUtil.show(this, getString(R.string.erry));
        insuraFragment.setLoadFail();

    }

    @Override
    public void erry(String result) {
        dismissLoading();

        ToastUtil.show(this, result);

        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            insuraFragment.setLoadFail();
            insuraFragment.setNoResult();
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected PagePresenter createPresenter() {
        presenter = new PagePresenter(this);
        return presenter;
    }

    /**
     * 判断是否有网
     */
    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(this)) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }

    @Override
    public void againRequest() {
//        if (isNetWork()) {
//            initData();
//        } else {
//            ToastUtil.show(this, R.string.pull_to_refresh_network_error);
//        }
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onRefresh(boolean isPullDown) {

    }

    @Override
    public void onLoadMore(boolean isSilence) {

    }

    @Override
    public void onRelease(float direction) {

    }

    @Override
    public void onHeaderMove(double headerMovePercent, int offsetY) {

    }

    /**
     * 日期选择
     */
    public void getDate() {
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        final int[] date = {0, 0};
        DateDialog birthDiolog = new DateDialog(this,
                new DateDialog.PriorityListener() {
                    @Override
                    public void refreshPriorityUI(String yearss, String monthss, String datass) {
                        // TODO Auto-generated method stub
                        year = yearss;
                        mouth = monthss;
                        dates = (yearss + "-" + monthss);
                        sginDate.setText(dates);
                        initData();
                    }
                }, date[0], date[1], width, height);
        Window window = birthDiolog.getWindow();
        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
        birthDiolog.setCancelable(true);
        birthDiolog.show();
    }
}
